package httpbot;

public class RequestBuilder{
	
	private static final String BASE_VALUE = "http://en.wikipedia.org/w/api.php?";
	
	private static final String SPLIT = "&";
	private static final String ACTION = "action=";
	private static final String PROP = "prop=";
	private static final String RVPROP = "rvprop=";
	private static final String FORMAT = "format=";
	private static final String TITLES = "titles=";
	
	private int fieldsAdded;
	
	private StringBuilder request;
	
	public RequestBuilder(){
		request = new StringBuilder();
		request.append(BASE_VALUE);
		fieldsAdded = 0;
	}
	
	public void clear(){
		request = new StringBuilder();
		request.append(BASE_VALUE);
		fieldsAdded = 0;
	}
	
	public String create(){
		return request.toString();
	}
	
	private RequestBuilder setField(String field, String value){
		if (fieldsAdded > 0){
			request.append(SPLIT);
		}
		request.append(field);
		request.append(value);
		fieldsAdded++;
		return this;
	}
	
	public RequestBuilder setAction(String action){
		return setField(ACTION, action);
	}
	
	public RequestBuilder setProp(String prop){
		return setField(PROP, prop);
	}
	
	public RequestBuilder setRvprop(String rvprop){
		return setField(RVPROP, rvprop);
	}
	
	public RequestBuilder setFormat(String format){
		return setField(FORMAT, format);
	}
	
	public RequestBuilder setTitles(String titles){
		return setField(TITLES, titles);
	}
	
}